package com.sjtucook;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class DishListActivity extends Activity {
	public static final String FLAG = "flag";
	public static final String FAVORITE = "favorite";
	public static final String RATE = "rate";
	public static final String HISTORY = "history";
	public static final String SEARCH = "search";
	public static final String DISH = "dish";
	public static final String QUERY = "query";
	public static final String DISHSORT = "dishsort";
	private Button search;
	private Button favorite;
	private Button toprated;
	private Button history;
	private Button back;
	private TextView titleText;
	private LinearLayout content;
	private CookApplication currentApp;
	private boolean fromsecond = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.content);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		content = (LinearLayout) findViewById(R.id.center_pane);
		this.titleText = (TextView) findViewById(R.id.tittle_text);
		// search button
		this.search = (Button) findViewById(R.id.tittle_bt);
		this.search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onSearchRequested();
			}
		});
		// favorite button
		this.favorite = (Button) findViewById(R.id.bottom_button_1);
		this.favorite.setText(R.string.FAVORITE);
		this.favorite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Cursor cursor = currentApp.getDbadapter().queryFavorite();
				addContentView(cursor);
				titleText
				.setText(getResources().getString(R.string.app_name)
						+ " — "
						+ getResources().getString(R.string.FAVORITE));
			}

		});
		// toprated button
		this.toprated = (Button) findViewById(R.id.bottom_button_2);
		this.toprated.setText(R.string.TOP_RATED);
		this.toprated.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Cursor cursor = currentApp.getDbadapter().queryTop();
				addContentView(cursor);
				titleText.setText(getResources().getString(
						R.string.app_name)
						+ " — "
						+ getResources().getString(R.string.TOP_RATED));
			}

		});
		// history button
		this.history = (Button) findViewById(R.id.bottom_button_3);
		this.history.setText(R.string.HISTORY);
		this.history.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Cursor cursor = currentApp.getDbadapter().queryHistory();
				addContentView(cursor);
				titleText.setText(getResources().getString(
						R.string.app_name)
						+ " — " + getResources().getString(R.string.HISTORY));
			}

		});
		// update button
		this.back = (Button) findViewById(R.id.bottom_button_4);
		this.back.setText(R.string.BACK);
		this.back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (fromsecond == true) {
					onBackPressed();
				} else {
					Intent intent = new Intent(DishListActivity.this,
							FirstMenuActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}
			}

		});

		Bundle bundle = this.getIntent().getExtras();
		String flag = bundle.getString(DishListActivity.FLAG);
		Cursor cursor = null;
		// get CookApplication object
		currentApp = (CookApplication) this.getApplicationContext();
		if (flag.equals(SecondMenuActivity.Second)) {
			this.fromsecond = true;
			String id = bundle.getString(DBAdapter.KEY_ID);
			String column_name = bundle.getString(DBAdapter.KEY_COLUMN_NAME);
			// db query
			cursor = currentApp.getDbadapter().queryDish(column_name, id);
			// set time line of cursor
			this.titleText
			.setText(this.getResources().getString(R.string.app_name)
					+ " — "
					+ bundle.getString(DishListActivity.DISHSORT));

		} else if (flag.equals(DishListActivity.FAVORITE)) {
			cursor = currentApp.getDbadapter().queryFavorite();
			this.titleText
					.setText(this.getResources().getString(R.string.app_name)
							+ " — "
							+ this.getResources().getString(R.string.FAVORITE));
		} else if (flag.equals(DishListActivity.RATE)) {
			cursor = currentApp.getDbadapter().queryTop();
			this.titleText.setText(this.getResources().getString(
					R.string.app_name)
					+ " — "
					+ this.getResources().getString(R.string.TOP_RATED));
		} else if (flag.equals(DishListActivity.HISTORY)) {
			cursor = currentApp.getDbadapter().queryHistory();
			this.titleText.setText(this.getResources().getString(
					R.string.app_name)
					+ " — " + this.getResources().getString(R.string.HISTORY));
		} else if (flag.equals(DishListActivity.SEARCH)) {
			cursor = currentApp.getDbadapter().searchDish(
					bundle.getString(DishListActivity.QUERY));
			this.titleText.setText(this.getResources().getString(
					R.string.app_name)
					+ " — "
					+ this.getResources().getString(R.string.SEARCH)
					+ " : " + bundle.getString(DishListActivity.QUERY));
		}

		addContentView(cursor);

	}

	private void addContentView(Cursor cursor) {
		if (cursor == null)
			return;
		content.removeAllViews();
		this.startManagingCursor(cursor);
		if (cursor.getCount() > 0) {
			// fill the contnet with a gridview
			ListView listView = new ListView(this);
			listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			listView.setScrollBarStyle(GridView.SCROLLBARS_OUTSIDE_OVERLAY);
			listView.setSelector(R.color.white);
			listView.setSmoothScrollbarEnabled(true);
			listView.setScrollingCacheEnabled(false);
			listView.setHorizontalFadingEdgeEnabled(false);
			listView.setVerticalFadingEdgeEnabled(false);
			listView.setDivider(null);
			content.addView(listView);
			// 绑定数据源
			listView.setAdapter(new Adapter(this, R.layout.griditem, cursor,
					new String[] {}, new int[] {}));
			// 绑定事件触发
			listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Cursor cursor = (Cursor) parent.getItemAtPosition(position);
					Dish dish = new Dish();
					int index = 0;

					index = cursor.getColumnIndex(DBAdapter.KEY_ID);
					dish.setId(cursor.getInt(index));
					index = cursor.getColumnIndex(DBAdapter.KEY_NAME);
					dish.setName(cursor.getString(index));
					index = cursor.getColumnIndex(DBAdapter.KEY_MATERIAL);
					dish.setMaterial(cursor.getString(index));
					index = cursor.getColumnIndex(DBAdapter.KEY_IMAGE);
					dish.setImage(cursor.getString(index));
					index = cursor.getColumnIndex(DBAdapter.KEY_DESCRIPTION);
					dish.setDescription(cursor.getString(index));
					index = cursor.getColumnIndex(DBAdapter.KEY_RATE);
					dish.setRate(cursor.getInt(index));
					index = cursor.getColumnIndex(DBAdapter.KEY_FAVORITE);
					dish.setFavorite(cursor.getInt(index));

					Intent intent = new Intent(DishListActivity.this,
							DishDetailActivity.class);
					Bundle bundle = new Bundle();
					bundle.putParcelable(DishListActivity.DISH, dish);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});
		} else {
			// no sort category
			TextView textView = new TextView(this);
			textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			textView.setGravity(Gravity.CENTER);
			textView.setText("No Item Found");
			content.addView(textView);
		}
	}

	private class Adapter extends SimpleCursorAdapter {

		public Adapter(Context context, int layout, Cursor c, String[] from,
				int[] to) {
			super(context, layout, c, from, to);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			RelativeLayout rl = (RelativeLayout) view;
			int name_index = cursor.getColumnIndex(DBAdapter.KEY_NAME);
			int image_index = cursor.getColumnIndex(DBAdapter.KEY_IMAGE);
			int in = cursor.getColumnIndex(DBAdapter.KEY_FAVORITE);
			int ins = cursor.getColumnIndex(DBAdapter.KEY_RATE);

			ImageView image = (ImageView) rl.findViewById(R.id.ItemImage);
			TextView text = (TextView) rl.findViewById(R.id.ItemText);
			image.setImageDrawable(getResources().getDrawable(R.drawable.icon));
			text.setText(cursor.getString(name_index));
		}

	}

}
